<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/3
 * Time: 10:53
 */

namespace app\applet\model;
use think\Db;
use think\Exception;
use Stbz\Api\SupplyClient;
class Notify
{
    private $appKey   = 'A855F0BA1EBCD119BEFBDE6EC07ECE65';
    private $appSecret = '68B892679EB78F9FB45ED3D62DE838B7';
    //转币回调
    public function transfer($input){
          $trans_sn=$input['trans_sn'];
          //获取当前转换记录
           $memberTransfer=Db::name("member_transfer")->where(['trans_sn'=>$trans_sn])->find();
          if($memberTransfer['status']==1){//待转账
              $transfer_num=$memberTransfer['transfer_num'];
              $pay_num=$memberTransfer['pay_num'];
              $member_id=$memberTransfer['member_id'];
              $receive_id=$memberTransfer['receive_id'];
              $relation_id=$memberTransfer['trans_id'];
              $tm=time();
              Db::startTrans();
              try{
                  //更新状态
                  $num=Db::name("member_transfer")->where(['trans_sn'=>$trans_sn])->update(['status'=>2]);
                  if($num<=0){
                      throw  new Exception("操作失败");
                  }
                  if($memberTransfer['type']==1){//积分
                      //写入记录
                      $asset_log=[];
                      $asset_log[] = ['member_id' => $member_id, 'type' => 2, 'amount' =>$pay_num ,
                          'source_type' => 2, 'description' => "会员转账", "change_type" => 1, 'log_type_id' => 13,
                          'relation_id' => $relation_id, 'create_time' => $tm, 'update_time' => $tm];
                      $receive_mobile=$memberTransfer['receive_mobile'];
                      $receive_id=$memberTransfer['receive_id'];
                      $asset_log[] = ['member_id' => $receive_id, 'type' => 1, 'amount' =>$transfer_num ,
                          'source_type' => 2, 'description' => "会员转账",'transfer_mobile'=>$receive_mobile,"transfer_id"=>$receive_id, "change_type" => 1, 'log_type_id' => 2,
                          'relation_id' =>  $relation_id, 'create_time' => $tm, 'update_time' => $tm];
                      $num=Db::name("member_asset_log")->insertAll($asset_log);
                      if($num<=0){
                          throw new Exception("更新失败10");
                      }
                      //删除记录
                      $num=Db::name("member_asset_log")->where(['log_type_id'=>55,'member_id'=>$member_id,'relation_id'=>$relation_id])->delete();
                      if($num<=0){
                          throw new Exception("更新失败10");
                      }
                      $score_trans_frozen=0;
                      if($memberTransfer['trans_type']==2){
                          $score_trans_frozen=$pay_num;
                      }
                      //扣除
                      $sql="update mm_member_asset set frozen_score=frozen_score-$pay_num,score_trans_frozen=score_trans_frozen-$score_trans_frozen,pay_frozen_score=pay_frozen_score-$pay_num where member_id=".$member_id." and frozen_score>=$pay_num and pay_frozen_score>=$pay_num and score_trans_frozen>=$score_trans_frozen ";
                      $num=Db::query($sql);
                      if($num<=0){
                          throw  new Exception("操作失败");
                      }
                      //新增
                      $sql="update mm_member_asset set score=score+$transfer_num where member_id=$receive_id" ;
                      $num=Db::query($sql);
                      if($num<=0){
                          throw  new Exception("操作失败");
                      }
                  }elseif($memberTransfer['type']==2){//金币
                      $asset_log=[];

                      $asset_log[] = ['member_id' => $member_id, 'type' => 2, 'amount' =>$pay_num ,
                          'source_type' => 2, 'description' => "会员转账", "change_type" => 2, 'log_type_id' => 32,
                          'relation_id' => $relation_id, 'create_time' => $tm, 'update_time' => $tm];
                      $receive_mobile=$memberTransfer['receive_mobile'];
                      $receive_id=$memberTransfer['receive_id'];
                      $asset_log[] = ['member_id' => $receive_id, 'type' => 1, 'amount' =>$transfer_num ,
                          'source_type' => 2, 'description' => "会员转账",'transfer_mobile'=>$receive_mobile,"transfer_id"=>$receive_id, "change_type" => 2, 'log_type_id' => 25,
                          'relation_id' =>  $relation_id, 'create_time' => $tm, 'update_time' => $tm];
                      $num=Db::name("member_asset_log")->insertAll($asset_log);
                      if($num<=0){
                          throw new Exception("更新失败10");
                      }
                      //删除记录

                       $num=Db::name("member_asset_log")->where(['log_type_id'=>56,'member_id'=>$member_id,'relation_id'=>$relation_id])->delete();
                      if($num<=0){
                          throw new Exception("更新失败10");
                      }
                      //扣除
                      $sql="update mm_member_asset set frozen_score=frozen_score-$pay_num where member_id=".$member_id." and frozen_score>=$pay_num  ";
                      $num=Db::query($sql);
                      if($num<=0){
                          throw  new Exception("操作失败");
                      }
                      //新增
                      $sql="update mm_member_asset set coin=coin+$transfer_num where member_id=$receive_id" ;
                      $num=Db::query($sql);
                      if($num<=0){
                          throw  new Exception("操作失败");
                      }
                  }
                  Db::commit();
                  returnJson(200,'操作成功');
              }catch (Exception $e){
                  $msg=$e->getMessage();
                  if(empty($msg)){
                      $msg="更新失败";
                  }
                  Db::rollback();
                  returnJson(-2004,$msg);
              }


          }

    }

    //订单回调
    public function order($input){
      $orderSn=$input['order_sn'];

       //获取订单信息
       $orderInfo=Db::name("order")->where(['order_Sn'=>$orderSn])
           ->field("order_id,member_id,is_del,status,pay_status,order_score_money,order_sn,
           order_coin_money,order_delivery_fee,wx_money,score_money,coin_money,source_type,ma_id,is_order")->find();
       $orderId=$orderInfo['order_id'];

       if(!in_array($orderInfo['status'],[2,7])&&$orderInfo['is_del']!=2&&$input['wx_pay_money']==$orderInfo['wx_money']){

           if(($orderInfo['order_score_money']+$orderInfo['order_coin_money']+$orderInfo['order_delivery_fee'])==($orderInfo['coin_money']+$orderInfo['wx_money']+$orderInfo['score_money'])){//订单金额判断
               $member_id=$orderInfo['member_id'];
               $coin_money=$orderInfo['coin_money'];
               $score_money=$orderInfo['score_money'];
               if($score_money>0) {
                   $cmp_score_money=Db::name("member_asset_log")
                       ->where(['member_id'=>$member_id,'log_type_id'=>53,'relation_id'=>$orderId])
                       ->value("amount");
                   if($cmp_score_money!=$score_money){
                       returnJson(-2004,"订单异常");
                   }
               }
               if($coin_money>0){
                   $cmp_coin_money=Db::name("member_asset_log")
                       ->where(['member_id'=>$member_id,'log_type_id'=>54,'relation_id'=>$orderId])
                       ->value("amount");
                   if($coin_money!=$cmp_coin_money){
                       returnJson(-2004,"订单异常");
                   }
                   
               }
               $tm=time();

               Db::startTrans();

               try{

                   $updOrder=['status'=>'2','pay_status'=>2,'pay_time'=>time(),'is_del'=>1,'update_time'=>$tm];
                   //获取配送方式
                   $d_method=Db::name("order_goods")->where(['order_id'=>$orderId])->sum("d_method");
                   if($orderInfo['score_money']>0||$orderInfo['coin_money']>0){
                       $num=Db::name("member_asset_log")
                           ->where(['member_id'=>$member_id,'log_type_id'=>['in',[53,54]],'relation_id'=>$orderId])
                           ->delete();
                       if($num<=0){
                           throw new Exception("更新失败11");
                       }
                   }

                   if($orderInfo['wx_money']>0){//生成微信支付订单记录
                       $asset_log[] = ['member_id' => $member_id, 'type' => 2, 'amount' =>$orderInfo['wx_money'] ,
                           'source_type' => 2, 'description' => "商城购物", "change_type" => 3, 'log_type_id' => 39,
                           'relation_id' => $orderId, 'create_time' => $tm, 'update_time' => $tm];

                   }
                   if($score_money>0) {
                       $asset_log[] = ['member_id' => $member_id, 'type' => 2, 'amount' =>$score_money ,
                           'source_type' => 2, 'description' => "商城购物", "change_type" => 1, 'log_type_id' => 17,
                           'relation_id' => $orderId, 'create_time' => $tm, 'update_time' => $tm];

                   }
                   if($coin_money>0){
                       $asset_log[] = ['member_id' => $member_id, 'type' => 2, 'amount' =>$coin_money,
                           'source_type' => 2, 'description' => "兑换商品", "change_type" => 2, 'log_type_id' => 33,
                           'relation_id' => $orderId, 'create_time' => $tm, 'update_time' => $tm];
                   }

                   $num=Db::name("member_asset_log")->insertAll($asset_log);
                   if($num<=0){
                       throw new Exception("更新失败10");
                   }
                   if($d_method==0){//不发货
                       $updOrder['status']=4;//待评价
                   }

                   //更新库存
                   $goodsList=Db::name("order_goods")
                       ->where(['order_id'=>$orderId,'d_method'=>['neq',0],'goods_num'=>['gt',0]])
                       ->field("goods_id,d_method,spec_id,goods_num")
                       ->select();
                   foreach ($goodsList as $goods){
                       $spec_id=$goods['spec_id'];
                       //更新送送货状态
                       if(in_array($goods['d_method'],array(0,2))){
                           $num=Db::name("order_goods")
                               ->where(['order_id'=>$orderId,'spec_id'=>$spec_id])
                               ->update(['og_d_status'=>4,'is_recevied'=>1]);
                           if($num<=0){
                               throw new Exception("更新失败12");
                           }
                       }
                       if(!in_array($orderInfo['source_type'],[2,3])){
                           //判断当前库存是否存在
                           $nums=Db::name("goods_spec")->where(['gs_id'=>$spec_id])->count();
                           if($nums>0){
                               $num=Db::name("goods_spec")->where(['gs_id'=>$spec_id])
                                   ->setDec("num",$goods['goods_num']);
                               if($num<=0){
                                   throw new Exception("更新失败9");
                               }
                           }
                       }
                   }
                   //删除购物车
                   $goodsArr=array_column($goodsList,"goods_id");

                   $nums=Db::name("cart")->where(['goods_id'=>['in',$goodsArr],'member_id'=>$member_id])->count();

                   if($nums>0){
                       $num=Db::name("cart")->where(['goods_id'=>['in',$goodsArr],'member_id'=>$member_id])->delete();
                       if($num<=0){
                           throw new Exception("更新失败71");
                       }
                   }
                   //更新订单状态
                   $num=Db::name("order")->where(['order_id'=>$orderId])->update( $updOrder);
                   if($num<=0){
                       throw new Exception("更新失败8");
                   }
                   if($coin_money>0||$score_money>0){
                       $sql="update mm_member_asset set frozen_score=frozen_score-$score_money,pay_frozen_score=pay_frozen_score-$score_money,frozen_coin=frozen_coin-$coin_money where member_id=".$member_id." and frozen_score>=$score_money  and pay_frozen_score>=$score_money and frozen_coin>=$coin_money ";

                       $num=Db::query($sql);
                       if($num<=0){
                           throw new Exception("更新失败7");
                       }
                   }
                   //更新销量
                   $isComplete=true;
                   $orderGoods=Db::name("order_goods")
                       ->where(['order_id'=>$orderId])
                       ->field("goods_id,spec_id,goods_num")
                       ->select();
                   if(!in_array($orderInfo['source_type'],[2,3])){
                       foreach ($orderGoods as $ogoods){
                           Db::name("goods")
                               ->where(['goods_id'=>$ogoods['goods_id']])
                               ->setInc("sale_total",$ogoods['goods_num']);
                           if($orderInfo['source_type']==4){//干货分享
                               Db::name("course_buy_record")
                                   ->insert(['goods_id'=>$ogoods['goods_id'],'order_id'=>$orderInfo['order_id'],
                                       'member_id'=>$member_id,'create_time'=>$tm]);
                           }
                           $gType=Db::name("goods")
                               ->where(['goods_id'=>$ogoods['goods_id']])
                               ->value('g_type');
                           if($orderInfo['source_type']!=4||$gType!=5){
                               $isComplete=false;
                           }
                       }
                   }else{
                       $isComplete=false;
                       foreach ($orderGoods as $ogoods){
                           Db::name("choice_goods")
                               ->where(['goods_id'=>$ogoods['goods_id']])
                               ->setInc("sale",$ogoods['goods_num']);

                       }
                   }
                   if($isComplete){
                       $nums=Db::name("order")->where(['order_id'=>$orderId])->update(['status'=>12]);
                       if($nums<=0){
                           throw new Exception("更新失败7");
                       }
                   }

                   //订单分销
                   $this->orderDistribution($orderId,$orderInfo['source_type']);
                   Db::commit();
                   returnJson(200,'订单更新成功');
               }catch (Exception $e){
                   $msg=$e->getMessage();
                   if(empty($msg)){
                       $msg="更新失败";
                   }
                   Db::rollback();
                   returnJson(-2004,$msg);
               }
               //供应链下单
              // $this->submitOrder($orderInfo);
           }

       }

   }

    /**
     * 供应链下单
     */
    public function submitOrder($orderInfo){

        $orderSn=$orderInfo['order_sn'];

        $member_id=$orderInfo['member_id'];
        $orderGoods=Db::name("order_goods")->where(['order_id'=>$orderInfo['order_id']])->field("goods_id,spec_id,goods_num")->select();
        //供应链下单
        if(in_array($orderInfo['source_type'],[2,3])&&empty($orderInfo['is_order'])) {
            vendor("JuHe.autoload");
            $spuList=[];
            $ma_id=$orderInfo['ma_id'];
            $address=Db::name("member_address")->where(['ma_id'=>$ma_id,'member_id'=>$member_id])->find();
            $province = $address['province'];
            $city = $address['city'];
            $area = $address['county'];
            $street = empty($address['address_detail']) ? $address['county'] : $address['address_detail'];
            foreach ($orderGoods as $goods){
                $spuList[]=[
                    'sku'=>$goods['spec_id'],
                    'number'=>$goods['goods_num'],
                ];
            }
            $orderParam =[
                'address'=>[
                    'consignee' => $address['m_name'],
                    'phone' => $address['m_mobile'],
                    'province' => $province,
                    'city' => $city,
                    'area' => $area,
                    'street' => $street,
                    'description' => $street,
                ],
                'spu'=>$spuList,
                'orderSn'=>$orderSn
            ];
            Db::startTrans();
            try {
                $num=Db::name("order")->where(['order_sn'=>$orderSn])->update(['is_order'=>1]);
                if($num<=0){
                    throw new Exception("订单创建失败");
                }

                $orderParam['orderSn']=$orderSn;//商城订单号
                $this->client = new SupplyClient( $this->appKey, $this->appSecret);
                $response = $this->client->getApiResponse("POST","/v2/order", $orderParam);
                $response =json_decode($response ,true);
                if($response['code']!=1&&$response['code']!=3002){
                     throw new Exception("订单创建失败");
                }
                Db::commit();
            }catch (Exception $e){
               Db::rollback();
            }
        }

    }

    /**
     * 订单分销
     */
    public function orderDistribution($orderID,$source_type)
    {
        //获取商品列表
        if(!in_array($source_type,[2,3])){
            $goodsOrder = Db::name("order_goods")->alias("og")
                ->join("mm_order o", "og.order_id=o.order_id")
                ->join("mm_goods g", "g.goods_id=og.goods_id")
                ->join("mm_goods_info gn", "g.goods_id=gn.goods_id")
                ->field("o.member_id,o.store_id,g.goods_id,g.g_type,g.source_type,og.goods_num,
            og.score_money,og.proportion,og.give_coin,og.copper,og.silver,og.golden,
            og.activity_direct_push,og.activity_inter_push,og.give_coin,og.og_id,
            og.activity_buyer,og.activity_company,og.activity_invest,og.activity_plateform,
            gn.gold_money,gn.silver_money,gn.copper_money")
                ->where(['o.is_give' => ['neq', 1],'og.order_id'=>$orderID])
                ->select();
        }else{
            $goodsOrder = Db::name("order_goods")->alias("og")
                ->join("mm_order o", "og.order_id=o.order_id")
                ->join("mm_choice_goods g", "g.goods_id=og.goods_id")
                ->field("o.member_id,o.store_id,g.goods_id,o.source_type,og.goods_num,
            og.score_money,og.proportion,og.give_coin,og.copper,og.silver,og.golden,
            og.activity_direct_push,og.activity_inter_push,og.give_coin,og.og_id,
            og.activity_buyer,og.activity_company,og.activity_invest,og.activity_plateform")
                ->where(['o.is_give' => ['neq', 1],'og.order_id'=>$orderID])
                ->select();
        }
        if (!empty($goodsOrder)) {
            //获取用户信息
            $member_id = $goodsOrder[0]['member_id'];

            $member = Db::name("member")->where(['member_id' => $member_id])->field("uid_1,uid_2")->find();
            //获取商家信息
            $store_id = $goodsOrder[0]['store_id'];

            $store_member_id = Db::name("store_apply")->where(['sa_id' => $store_id])->value("member_id");;
            $tm = time();

//            Db::startTrans();
//            try {
                foreach ($goodsOrder as $order) {
                    if(in_array($source_type,[2,3])){
                        $order['g_type']=0;
                    }
                    $og_id=$order['og_id'];
                    //1=消费爆品，2=裂变爆品,3=返利，4=活动,5=讲坛'
                    $gType = $order['g_type'];
                    if ($gType == 1) {
                        continue;
                    }elseif($order['source_type']==4){//干货分享
                        if($gType==2){//爆品

                            //获取商品详情
                            $nums=Db::name("goods_member_grade")->where(['member_id'=>$member_id,'goods_id'=>$order['goods_id']])->count();
                            if(empty($nums)){
                                $gmGrade=['member_id'=>$member_id,'goods_id'=>$order['goods_id'],'member_grade'=>1,'create_time'=>$tm,'update_time'=>$tm];
                                $num=Db::name("goods_member_grade")->insert($gmGrade);
                                if($num<=0){
                                    throw new Exception("更新失败2");
                                }
                            }
                            //获取当前会员等级
                            $goods_member_grade=Db::name("goods_member_grade")->where(['member_id'=>$member_id,'goods_id'=>$order['goods_id']])->value("member_grade");
                            //判断当前订单是否升级
                            $orderAmount=$order['score_money']*$order['goods_num'];
                            $copper_money=$order['copper_money'];
                            $silver_money=$order['silver_money'];
                            $gold_money=$order['gold_money'];
                            if ($orderAmount >= $gold_money&&$gold_money>0) {
                                $grade = 4;
                            } elseif ($orderAmount >= $silver_money&&$silver_money>0) {
                                $grade = 3;

                            } elseif ($orderAmount >= $copper_money&&$copper_money>0) {
                                $grade = 2;

                            } else {
                                $grade = 1;
                            }

                            $new_goods_member_grade=$goods_member_grade>$grade?$goods_member_grade:$grade;

                            //获取上级会员等级
                            $inviter_id=Db::name("goods_member_inviter")->where(['member_id'=>$member_id,'goods_id'=>$order['goods_id']])->value("inviter_id");


                            $is_grade=0;
                            if($goods_member_grade< $grade){//会员需要升级
                                $is_grade=1;
                                if(empty($inviter_id)){//无邀请人 直接升级会员
                                    $is_grade=0;
                                    $num=Db::name("goods_member_grade")->insert(['goods_id'=>$order['goods_id'],'member_id'=>$member_id,'member_grade'=>$grade,'create_time'=>$tm,'update_time'=>$tm]);
                                    if($num<=0){
                                        throw new Exception("更新失败4");
                                    }
                                    $goods_member_grade=$new_goods_member_grade;
                                }
                            }

                            if( $inviter_id>0){//存在邀请人
                                $direct_member_grade=Db::name("goods_member_grade")->where(['member_id'=>$inviter_id,'goods_id'=>$order['goods_id']])->value("member_grade");
                                if($direct_member_grade==3&&$order['golden']>0){//金客
                                    $dst_status=1;
                                    $direct_push_score = $order['score_money']*$order['goods_num'] * $order['golden'] / 100;
                                    $asset_log[] = ['member_id' => $inviter_id, 'type' => 1, 'amount' => $direct_push_score,
                                        'source_type' => 2, 'description' => "商城消费推荐奖", "change_type" => 1, 'log_type_id' => 5,
                                        'relation_id' => $orderID,'relation_sid'=>$og_id, 'create_time' => $tm, 'update_time' => $tm,'handle_type'=>6,'release_status'=>0];
                                    $num=Db::name("member_asset")->where(['member_id' => $member['uid_1']])->setInc("frozen_score", $direct_push_score);
                                    if($num<=0){
                                        throw new Exception("更新失败4");
                                    }

                                }elseif($direct_member_grade==2&&$order['silver']>0){//银客
                                    if($new_goods_member_grade>$direct_member_grade){//下级等级比上级高
                                        $dst_status=4;
                                    }else{
                                        $dst_status=1;
                                        $direct_push_score = $order['score_money'] *$order['goods_num']* $order['silver'] / 100;
                                        $asset_log[] = ['member_id' => $inviter_id, 'type' => 1, 'amount' => $direct_push_score,
                                            'source_type' => 2, 'description' => "商城消费推荐奖", "change_type" => 1, 'log_type_id' => 5,
                                            'relation_id' => $orderID,'relation_sid'=>$og_id, 'create_time' => $tm, 'update_time' => $tm,'handle_type'=>6,'release_status'=>0];
                                        $num=Db::name("member_asset")->where(['member_id' => $member['uid_1']])->setInc("frozen_score", $direct_push_score);
                                        if($num<=0){
                                            throw new Exception("更新失败4");
                                        }
                                    }
                                }elseif($direct_member_grade==1&&$order['copper']>0){//铜客
                                    if($new_goods_member_grade>$direct_member_grade){//下级等级比上级高
                                        $dst_status=4;
                                    }else{
                                        $dst_status=1;
                                        $direct_push_score = $order['score_money'] *$order['goods_num']* $order['copper'] / 100;
                                        $asset_log[] = ['member_id' => $inviter_id, 'type' => 1, 'amount' => $direct_push_score,
                                            'source_type' => 2, 'description' => "商城消费推荐奖", "change_type" => 1, 'log_type_id' => 5,
                                            'relation_id' => $orderID,'relation_sid'=>$og_id, 'create_time' => $tm, 'update_time' => $tm,'handle_type'=>6,'release_status'=>0];
                                        $num=Db::name("member_asset")->where(['member_id' => $member['uid_1']])->setInc("frozen_score", $direct_push_score);
                                        if($num<=0){
                                            throw new Exception("更新失败4");
                                        }
                                    }
                                }else{
                                    $dst_status=4;
                                }
                            }else{
                                $dst_status=3; //邀请人不存在 无需分销
                            }
                            if($dst_status==1){
                                $dst_grade=$direct_member_grade; //参与分销的等级
                            }else{
                                $dst_grade=0;
                            }
                            if(empty($inviter_id)){//无邀请人 当自动审核
                                $exm_time=$tm;
                            }else{
                               $exm_time=0;
                            }
                            $num=Db::name("distribution_order_goods")->insertGetId(['member_id'=>$member_id,"dst_min_grade"=>$new_goods_member_grade,
                                "dst_grade"=>$dst_grade,"order_id"=>$orderID,"order_goods_id"=>$og_id,'dst_status'=>$dst_status,'is_grade'=>$is_grade,'is_real'=>2,
                                'goods_id'=>$order['goods_id'],'dst_member_id'=>$inviter_id,'create_time'=>$tm,'update_time'=>$tm,'exm_time'=>$exm_time]);
                            if($num<=0){
                                throw new Exception("更新失败");
                            }
                            //判断当前购买商品是否有下级订单可参与分销 审核时间 或创建时间3天内有效
                            $dtm=$tm-3*24*3600;
                            $where1=['exm_time'=>['egt',$dtm]];
                            $where2=['create_time'=>['egt',$dtm]];
                            $dstList=Db::name("distribution_order_goods")
                                ->where(['dst_status'=>['in',[2,4]],'is_real'=>2,'goods_id'=>$order['goods_id'],
                                    'dst_member_id'=>$order['member_id'],'dst_min_grade'=>['elt',$new_goods_member_grade]])
                                ->where(function ($query) use ( $where1,$where2) {
                                    $query->where(function($q) use ( $where1){
                                        $q->where( $where1);
                                    })->whereOR($where2);
                                })
                                ->select();
                            if(!empty($dstList)){
                                $dstAtt=[];
                                $dstgAtt=[];
                                foreach ($dstList as $dst){
                                    if(empty($inviter_id)){//没有推荐人
                                      //if($dst['dst_grade']<$new_goods_member_grade){
                                          $dstAtt[]=$dst['dst_id'];
                                          //直接分销
                                          $ogInfo=Db::name("order_goods")->where(['og_id'=>$dst['order_goods_id']])->find();
                                          if($new_goods_member_grade==4){//金客
                                              $rate=$ogInfo['golden'];
                                          }elseif($new_goods_member_grade==3){//银客
                                              $rate=$ogInfo['silver'];
                                          }elseif($new_goods_member_grade==2){//铜客
                                              $rate=$ogInfo['copper'];
                                          }
                                          $direct_push_score = ($ogInfo['score_money'] *$ogInfo['goods_num']+$ogInfo['coin_money']*$ogInfo['goods_num'])* $rate / 100;
                                          $asset_log = ['member_id' =>$member_id, 'type' => 1, 'amount' => $direct_push_score,
                                              'source_type' => 2, 'description' => "商城消费推荐奖", "change_type" => 1, 'log_type_id' => 5,
                                              'relation_id' => $orderID,'relation_sid'=>$og_id, 'create_time' => $tm, 'update_time' => $tm,'handle_type'=>6,'release_status'=>0];
                                          $num=Db::name("member_asset_log")->insert($asset_log);
                                          if($num<=0){
                                              throw new Exception("更新失败4");
                                          }
                                          $num=Db::name("member_asset")->where(['member_id' => $member_id])->setInc("frozen_score", $direct_push_score);
                                          if($num<=0){
                                              throw new Exception("更新失败4");
                                          }
                                     // }
                                    }else{
                                        $order_goods_ids=$dst['order_goods_ids'];
                                        $order_goods_ids=explode(",",$order_goods_ids);
                                        if(empty($order_goods_ids)){
                                            $order_goods_ids=$og_id;
                                        }else{
                                            array_unshift($order_goods_ids,$og_id);

                                            $order_goods_ids=implode(",",$order_goods_ids);
                                        }
                                        $num=Db::name("distribution_order_goods")->where(['dst_id'=>$dst['dst_id']])->update(['order_goods_ids'=>$order_goods_ids]);//记录订单
                                        if($num<=0){
                                            throw new Exception("更新失败4");
                                        }
//                                        if($dst['dst_grade']<$goods_member_grade){
//                                            $dstgAtt[]=$dst['dst_id'];
//                                        }
                                    }
                                }
                                if(!empty($dstAtt)){
                                    $num=Db::name("distribution_order_goods")->where(['dst_id'=>['in',  $dstAtt]])->update(['dst_grade'=>$new_goods_member_grade,'dst_status'=>1]);//已分销
                                    if($num<=0){
                                        throw new Exception("更新失败4");
                                    }
                                }
//                                if(!empty($dstgAtt)){
//                                    $num=Db::name("distribution_order_goods")->where(['dst_id'=>['in',  $dstAtt]])->update(['dst_grade'=>$goods_member_grade]);
//                                    if($num<=0){
//                                        throw new Exception("更新失败4");
//                                    }
//                                }
                            }
                        }else{//返利
                            //是否消费送金币
                            $gold_coin_num = 0;
                            if($order['give_coin']>0){
                                $gold_coin_num = $order['give_coin'];
                            }
                            //获取订单商品信息
                            $direct_push_score = $order['activity_direct_push']*$order['goods_num'];
                            $inter_push_score = $order['activity_inter_push']*$order['goods_num'];
                            $buyer_score = $order['activity_buyer']*$order['goods_num'];
                            $asset_log = [];
                            //买家积分赠送
                            if ($gold_coin_num > 0) {
//                                //扣除商家金币
//                                $asset_log[] = ['member_id' => $store_member_id, 'type' => 2, 'amount' => $gold_coin_num,
//                                    'source_type' => 2, 'description' => "商品卖出", "change_type" => 2, 'log_type_id' => 57,
//                                    'relation_id' => $orderID,'relation_sid'=>$og_id,'handle_type'=>5, 'create_time' => $tm, 'update_time' => $tm,'release_status'=>0];
//                                $num=Db::name("member_asset")->where(['member_id' => $store_member_id, 'coin' => ['egt', $gold_coin_num]])->setDec("coin", $gold_coin_num);
//                                if($num<=0){
//                                    throw new Exception("更新失败");
//                                }
                                //赠送买家金币
                                $asset_log[] = ['member_id' => $member_id, 'type' => 1, 'amount' => $gold_coin_num,
                                    'source_type' => 2, 'description' => "商城消费推荐奖", "change_type" => 2, 'log_type_id' => 29,
                                    'relation_id' => $orderID,'relation_sid'=>$og_id,'handle_type'=>3, 'create_time' => $tm, 'update_time' => $tm,'release_status'=>0];
                                $num=Db::name("member_asset")->where(['member_id' => $member_id])->setInc("coin", $gold_coin_num);
                                if($num<=0){
                                    throw new Exception("更新失败");
                                }
                            }
                            if ($buyer_score > 0) {
                                $asset_log[] = ['member_id' => $member_id, 'type' => 1, 'amount' => $buyer_score,
                                    'source_type' => 2, 'description' => "商城消费返利奖", "change_type" => 1, 'log_type_id' => 73,
                                    'relation_id' => $orderID, 'relation_sid' => $og_id, 'handle_type' => 3, 'create_time' => $tm, 'update_time' => $tm, 'release_status' => 2];
                                $num = Db::name("member_asset")->where(['member_id' => $member_id])->setInc("frozen_score", $buyer_score);
                                if ($num <= 0) {
                                    throw new Exception("更新失败");
                                }
                            }
                            if ($direct_push_score>0&&$member['uid_1'] > 0) {//直推

                                $asset_log[] = ['member_id' => $member['uid_1'], 'type' => 1, 'amount' => $direct_push_score,
                                    'source_type' => 2, 'description' => "商城消费推荐奖", "change_type" => 1, 'log_type_id' => 5,
                                    'relation_id' => $orderID,'relation_sid'=>$og_id,'handle_type'=>3, 'create_time' => $tm, 'update_time' => $tm,'release_status'=>2];
                                $num=Db::name("member_asset")->where(['member_id' => $member['uid_1']])->setInc("frozen_score", $direct_push_score);
                                if($num<=0){
                                    throw new Exception("更新失败");
                                }
                            }
                            if ($inter_push_score>0&&$member['uid_2'] > 0) {//间推

                                $asset_log[] = ['member_id' => $member['uid_2'], 'type' => 1, 'amount' => $inter_push_score,
                                    'source_type' => 2, 'description' => "商城消费推荐奖", "change_type" => 1, 'log_type_id' => 5,
                                    'relation_id' => $orderID,'relation_sid'=>$og_id,'handle_type'=>3, 'create_time' => $tm, 'update_time' => $tm,'release_status'=>2];
                                $num=Db::name("member_asset")->where(['member_id' => $member['uid_2']])->setInc("frozen_score", $inter_push_score);
                                if($num<=0){
                                    throw new Exception("更新失败");
                                }
                            }
                            if (!empty($asset_log)) {
                                $num=Db::name("member_asset_log")->insertAll($asset_log);
                                if($num<=0){
                                    throw new Exception("更新失败");
                                }
                            }
                            $num=Db::name("order_goods")->where(['og_id'=>$og_id])->update(['give_status'=>4]);
                            if($num<=0){
                                throw new Exception("更新失败");
                            }
                        }


                    }elseif ($gType == 2) {//裂变商品
                        //获取商品详情
                       $nums=Db::name("goods_member_grade")->where(['member_id'=>$member_id,'goods_id'=>$order['goods_id']])->count();
                       if(empty($nums)){
                           $gmGrade=['member_id'=>$member_id,'goods_id'=>$order['goods_id'],'member_grade'=>1,'create_time'=>$tm,'update_time'=>$tm];
                           $num=Db::name("goods_member_grade")->insert($gmGrade);
                           if($num<=0){
                               throw new Exception("更新失败2");
                           }
                       }
                       //获取当前会员等级
                        $goods_member_grade=Db::name("goods_member_grade")->where(['member_id'=>$member_id,'goods_id'=>$order['goods_id']])->value("member_grade");
                       //判断当前订单是否升级
                        $orderAmount=$order['score_money']*$order['goods_num'];
                        $copper_money=$order['copper_money'];
                        $silver_money=$order['silver_money'];
                        $gold_money=$order['gold_money'];
                        if ($orderAmount >= $gold_money&&$gold_money>0) {
                            $grade = 4;
                            $gradeName='金客';
                        } elseif ($orderAmount >= $silver_money&&$silver_money>0) {
                            $grade = 3;
                            $gradeName='银客';
                        } elseif ($orderAmount >= $copper_money&&$copper_money>0) {
                            $grade = 2;
                            $gradeName='铜客';
                        } else {
                            $grade = 1;
                        }
                        $is_grade=0;
                       if($goods_member_grade< $grade){//会员需要升级
                           $is_grade=1;
                       }
                       $new_goods_member_grade=$goods_member_grade>$grade?$goods_member_grade:$grade;

                       //获取上级会员等级
                        $inviter_id=Db::name("goods_member_inviter")->where(['member_id'=>$member_id,'goods_id'=>$order['goods_id']])->value("inviter_id");
                        if($inviter_id>0){
                            $direct_member_grade=Db::name("goods_member_grade")->where(['member_id'=>$inviter_id,'goods_id'=>$order['goods_id']])->value("member_grade");
                            if($direct_member_grade==3&&$order['golden']>0){//金客
                                $dst_status=1;
                                $direct_push_score = $order['score_money']*$order['goods_num'] * $order['golden'] / 100;
                                $asset_log[] = ['member_id' => $inviter_id, 'type' => 3, 'amount' => $direct_push_score,
                                    'source_type' => 2, 'description' => "商城消费推荐奖", "change_type" => 1, 'log_type_id' => 58,
                                    'relation_id' => $orderID,'relation_sid'=>$og_id, 'create_time' => $tm, 'update_time' => $tm,'handle_type'=>2];
                                $num=Db::name("member_asset")->where(['member_id' => $member['uid_1']])->setInc("seven_frozen_score", $direct_push_score);
                                if($num<=0){
                                    throw new Exception("更新失败4");
                                }
                            }elseif($direct_member_grade==2&&$order['silver']>0){//银客
                                if($new_goods_member_grade>$direct_member_grade){//下级等级比上级高
                                    $dst_status=4;
                                }else{
                                    $dst_status=1;
                                    $direct_push_score = $order['score_money'] *$order['goods_num']* $order['silver'] / 100;
                                    $asset_log[] = ['member_id' => $inviter_id, 'type' => 3, 'amount' => $direct_push_score,
                                        'source_type' => 2, 'description' => "商城消费推荐奖", "change_type" => 1, 'log_type_id' => 58,
                                        'relation_id' => $orderID,'relation_sid'=>$og_id, 'create_time' => $tm, 'update_time' => $tm,'handle_type'=>2];
                                    $num=Db::name("member_asset")->where(['member_id' => $member['uid_1']])->setInc("seven_frozen_score", $direct_push_score);
                                    if($num<=0){
                                        throw new Exception("更新失败4");
                                    }
                                }
                            }elseif($direct_member_grade==1&&$order['copper']>0){//铜客
                                if($new_goods_member_grade>$direct_member_grade){//下级等级比上级高
                                    $dst_status=4;
                                }else{
                                    $dst_status=1;
                                    $direct_push_score = $order['score_money'] *$order['goods_num']* $order['copper'] / 100;
                                    $asset_log[] = ['member_id' => $inviter_id, 'type' => 3, 'amount' => $direct_push_score,
                                        'source_type' => 2, 'description' => "商城消费推荐奖", "change_type" => 1, 'log_type_id' => 58,
                                        'relation_id' => $orderID,'relation_sid'=>$og_id, 'create_time' => $tm, 'update_time' => $tm,'handle_type'=>2];
                                    $num=Db::name("member_asset")->where(['member_id' => $member['uid_1']])->setInc("seven_frozen_score", $direct_push_score);
                                    if($num<=0){
                                        throw new Exception("更新失败4");
                                    }
                                }
                            }
                        }else{
                            $dst_status=3; //邀请人不存在 无需分销
                        }
                        if($dst_status==1){
                            $dst_grade=$direct_member_grade; //参与分销的等级
                        }else{
                            $dst_grade=0;
                        }
                        if(empty($inviter_id)){//无邀请人 当自动审核
                            $exm_time=$tm;
                        }else{
                            $exm_time=0;
                        }
                        $num=Db::name("distribution_order_goods")->insertGetId(['member_id'=>$member_id,"dst_min_grade"=>$new_goods_member_grade,
                            "dst_grade"=>$dst_grade,"order_id"=>$orderID,"order_goods_id"=>$og_id,'dst_status'=>$dst_status,'is_grade'=>$is_grade,'is_real'=>1,
                            'goods_id'=>$order['goods_id'],'dst_member_id'=>$inviter_id,'create_time'=>$tm,'update_time'=>$tm,'exm_time'=>$exm_time]);
                        if($num<=0){
                            throw new Exception("更新失败");
                        }
                        //判断当前购买商品是否有下级订单可参与分销 审核时间 或创建时间3天内有效
                        $dtm=$tm-3*24*3600;
                        $where1=['exm_time'=>['egt',$dtm]];
                        $where2=['create_time'=>['egt',$dtm]];
                        $dstList=Db::name("distribution_order_goods")
                            ->where(['dst_status'=>['in',[2,4]],'is_real'=>1,'goods_id'=>$order['goods_id'],
                                'dst_member_id'=>$order['member_id'],'dst_min_grade'=>['elt',$new_goods_member_grade]])
                            ->where(function ($query) use ( $where1,$where2) {
                                $query->where(function($q) use ( $where1){
                                    $q->where( $where1);
                                })->whereOR($where2);
                            })
                            ->select();
                        if(!empty($dstList)) {
                            foreach ($dstList as $dst) {
                                $order_goods_ids = $dst['order_goods_ids'];
                                $order_goods_ids = explode(",", $order_goods_ids);
                                if (empty($order_goods_ids)) {
                                    $order_goods_ids = $og_id;
                                } else {
                                    array_unshift($order_goods_ids, $og_id);
                                    $order_goods_ids = implode(",", $order_goods_ids);
                                }
                                //更新下级未分销的记录 把当前商品订单号 添加到order_goods_ids 方便到时收货后七天进行分销
                                $num = Db::name("distribution_order_goods")->where(['dst_id' => $dst['dst_id']])->update(['order_goods_ids' => $order_goods_ids]);//记录订单
                                if ($num <= 0) {
                                    throw new Exception("更新失败4");
                                }
                            }
                        }

                        


                    }elseif ($gType == 5) {//讲坛
                        //是否消费送金币
                        $gold_coin_num = 0;
                        if($order['give_coin']>0){
                            $gold_coin_num = $order['give_coin'];
                        }
                        //获取订单商品信息
                        $direct_push_score = $order['activity_direct_push']*$order['goods_num'];
                        $inter_push_score = $order['activity_inter_push']*$order['goods_num'];
                        $buyer_score = $order['activity_buyer']*$order['goods_num'];
                        $asset_log = [];
                        //买家积分赠送
                        if ($gold_coin_num > 0) {
                            //扣除商家金币
                            $asset_log[] = ['member_id' => $store_member_id, 'type' => 2, 'amount' => $gold_coin_num,
                                'source_type' => 2, 'description' => "商品卖出", "change_type" => 2, 'log_type_id' => 57,
                                'relation_id' => $orderID,'relation_sid'=>$og_id,'handle_type'=>5, 'create_time' => $tm, 'update_time' => $tm,'release_status'=>0];
                            $num=Db::name("member_asset")->where(['member_id' => $store_member_id, 'coin' => ['egt', $gold_coin_num]])->setDec("coin", $gold_coin_num);
                            if($num<=0){
                                throw new Exception("更新失败");
                            }
                            //赠送买家金币
                            $asset_log[] = ['member_id' => $member_id, 'type' => 1, 'amount' => $gold_coin_num,
                                'source_type' => 2, 'description' => "商城消费推荐奖", "change_type" => 2, 'log_type_id' => 29,
                                'relation_id' => $orderID,'relation_sid'=>$og_id,'handle_type'=>5, 'create_time' => $tm, 'update_time' => $tm,'release_status'=>0];
                            $num=Db::name("member_asset")->where(['member_id' => $member_id])->setInc("coin", $gold_coin_num);
                            if($num<=0){
                                throw new Exception("更新失败");
                            }
                        }
                        if ($buyer_score > 0) {
                            $asset_log[] = ['member_id' => $member_id, 'type' => 1, 'amount' => $buyer_score,
                                'source_type' => 2, 'description' => "商城消费返利奖", "change_type" => 1, 'log_type_id' => 73,
                                'relation_id' => $orderID, 'relation_sid' => $og_id, 'handle_type' => 5, 'create_time' => $tm, 'update_time' => $tm, 'release_status' => 2];
                            $num = Db::name("member_asset")->where(['member_id' => $member_id])->setInc("frozen_score", $buyer_score);
                            if ($num <= 0) {
                                throw new Exception("更新失败");
                            }
                        }
                        if ($direct_push_score>0&&$member['uid_1'] > 0) {//直推

                            $asset_log[] = ['member_id' => $member['uid_1'], 'type' => 1, 'amount' => $direct_push_score,
                                'source_type' => 2, 'description' => "商城消费推荐奖", "change_type" => 1, 'log_type_id' => 5,
                                'relation_id' => $orderID,'relation_sid'=>$og_id,'handle_type'=>5, 'create_time' => $tm, 'update_time' => $tm,'release_status'=>2];
                            $num=Db::name("member_asset")->where(['member_id' => $member['uid_1']])->setInc("frozen_score", $direct_push_score);
                            if($num<=0){
                                throw new Exception("更新失败");
                            }
                        }
                        if ($inter_push_score>0&&$member['uid_2'] > 0) {//间推

                            $asset_log[] = ['member_id' => $member['uid_2'], 'type' => 1, 'amount' => $inter_push_score,
                                'source_type' => 2, 'description' => "商城消费推荐奖", "change_type" => 1, 'log_type_id' => 5,
                                'relation_id' => $orderID,'relation_sid'=>$og_id,'handle_type'=>5, 'create_time' => $tm, 'update_time' => $tm,'release_status'=>2];
                            $num=Db::name("member_asset")->where(['member_id' => $member['uid_2']])->setInc("frozen_score", $inter_push_score);
                            if($num<=0){
                                throw new Exception("更新失败");
                            }
                        }
                        if (!empty($asset_log)) {
                            $num=Db::name("member_asset_log")->insertAll($asset_log);
                            if($num<=0){
                                throw new Exception("更新失败");
                            }
                        }
                        $num=Db::name("order_goods")->where(['og_id'=>$og_id])->update(['give_status'=>4]);
                        if($num<=0){
                            throw new Exception("更新失败");
                        }
                    }else{
                        if($gType==3){
                            $handle_type=3;
                        }elseif($gType==4){
                            $handle_type=4;
                        }else{
                            $handle_type=0;
                        }
                        $gold_coin_num = 0;
                        if($order['give_coin']>0){
                            $gold_coin_num = $order['give_coin']*$order['goods_num'];
                        }
                        if ($gold_coin_num > 0) {
                            if($store_member_id>0){
                                //扣除商家金币
                                $asset_log[] = ['member_id' => $store_member_id, 'type' => 3, 'amount' => $gold_coin_num,
                                    'source_type' => 2, 'description' => "商品卖出", "change_type" => 2, 'log_type_id' => 60,
                                    'relation_id' => $orderID,'relation_sid'=>$og_id, 'create_time' => $tm, 'update_time' => $tm,'handle_type'=>$handle_type];
                                $sql="update mm_member_asset set coin=coin-$gold_coin_num,frozen_coin=frozen_coin+$gold_coin_num,seven_frozen_coin=seven_frozen_coin+$gold_coin_num where member_id=".$store_member_id." and coin>=$gold_coin_num ";
                                $num=Db::query($sql);
                                if($num<=0){
                                    throw new Exception("更新失败1");
                                }
                            }
                            //赠送买家金币
                            $asset_log[] = ['member_id' => $member_id, 'type' => 1, 'amount' => $gold_coin_num,
                                'source_type' => 2, 'description' => "商城消费", "change_type" => 2, 'log_type_id' => 29,
                                'relation_id' => $orderID,'relation_sid'=>$og_id, 'create_time' => $tm, 'update_time' => $tm,'handle_type'=>$handle_type];
                            $num=Db::name("member_asset")->where(['member_id' => $member_id])->setInc("seven_frozen_coin", $gold_coin_num);
                            if($num<=0){
                                throw new Exception("更新失败2");
                            }
                        }
                        //获取订单商品信息
                        $activity_direct_push = $order['activity_direct_push']*$order['goods_num'];
                        $activity_inter_push = $order['activity_inter_push']*$order['goods_num'];
                        $buyer_score = $order['activity_buyer']*$order['goods_num'];
                        $asset_log = [];
                        //买家积分赠送
                        if($buyer_score>0){
                            $asset_log[] = ['member_id' => $member_id, 'type' => 3, 'amount' => $buyer_score,
                                'source_type' => 2, 'description' => "商城消费", "change_type" => 1, 'log_type_id' => 47,
                                'relation_id' => $orderID,'relation_sid'=>$og_id, 'create_time' => $tm, 'update_time' => $tm,'handle_type'=>$handle_type];
                            $num=Db::name("member_asset")->where(['member_id' => $member_id])->setInc("seven_frozen_score", $buyer_score);
                            if($num<=0){
                                throw new Exception("更新失败3");
                            }
                        }
                        if ($member['uid_1'] > 0 && $activity_direct_push > 0) {//直推
                            $asset_log[] = ['member_id' => $member['uid_1'], 'type' => 3, 'amount' => $activity_direct_push,
                                'source_type' => 2, 'description' => "商城消费推荐奖", "change_type" => 1, 'log_type_id' => 58,
                                'relation_id' => $orderID,'relation_sid'=>$og_id, 'create_time' => $tm, 'update_time' => $tm,'handle_type'=>$handle_type];
                            $num=Db::name("member_asset")->where(['member_id' => $member['uid_1']])->setInc("seven_frozen_score", $activity_direct_push);
                            if($num<=0){
                                throw new Exception("更新失败");
                            }
                        }
                        if ($member['uid_2'] > 0 && $activity_inter_push > 0) {//间推
                            $asset_log[] = ['member_id' => $member['uid_2'], 'type' => 3, 'amount' => $activity_inter_push,
                                'source_type' => 2, 'description' => "商城消费推荐奖", "change_type" => 1, 'log_type_id' => 58,
                                'relation_id' => $orderID,'relation_sid'=>$og_id, 'create_time' => $tm, 'update_time' => $tm,'handle_type'=>$handle_type];
                            $num=Db::name("member_asset")->where(['member_id' => $member['uid_2']])->setInc("seven_frozen_score", $activity_inter_push);
                            if($num<=0){
                                throw new Exception("更新失败");
                            }
                        }
                        if (!empty($asset_log)) {
                            $num=Db::name("member_asset_log")->insertAll($asset_log);
                            if($num<=0){
                                throw new Exception("更新失败");
                            }
                        }
                        $num=Db::name("order_goods")->where(['og_id'=>$og_id])->update(['give_status'=>1]);
                        if($num<=0){
                            throw new Exception("更新失败");
                        }
                    }
                }
                //更新订单状态
                $num=Db::name("order")->where(['order_id' => $orderID])->update(['is_give' => 1, 'give_time' => $tm]);
                if($num<=0){
                    throw new Exception("更新失败");
                }
//                Db::commit();
//            } catch (Exception $e) {
//                Db::rollback();
//            }
        }
    }

    public function refund($input){
        $orderID=$input['order_id'];
        $ogId=$input['og_id'];
        //获取订单商品信息
        $orderGoods=Db::name("order_goods")->where(['og_id'=>$ogId,'order_id'=>$orderID])->find();
        //获取记录
        $assetLogList=Db::name("member_asset_log")->where(['relation_id' => $orderID,'change_type'=>3,'relation_sid'=>$ogId,'log_type_id'=>['in',['58,29,47,60']]])->select();
        Db::startTrans();
        try{
            //7天信誉记录资产处理
            foreach ($assetLogList as $log){
                $member_id=$log['member_id'];
                $log_type_id=$log['log_type_id'];
                $amount=$log['amount'];
                $mal_id=$log['mal_id'];
                $num=Db::name("member_asset_log")->where(['member_id'=>$member_id,'mal_id'=>$mal_id])->delete();
                if($num<=0){
                    throw new Exception("更新失败");
                }
                if(in_array($log_type_id,[58,47,29])){//删除推荐赠送 7天信誉冻结


                    $num=Db::name("member_asset")->where(['member_id'=>$member_id])->setDec("seven_frozen_score", $amount);
                    if($num<=0){
                        throw new Exception("更新失败");
                    }
                }elseif($log_type_id==60){
                    $sql="update mm_member_asset set coin=coin+$amount,frozen_coin=frozen_score-$amount,seven_frozen_coin=seven_frozen_coin-$amount where member_id=".$member_id."  and frozen_coin>=$amount and seven_frozen_coin>=$amount";
                    $num=Db::query($sql);
                    if($num<=0){
                        throw new Exception("更新失败");
                    }
                }
            }
            //退款
            //暂时全部退到积分金币里
            $score_money=$orderGoods['score_money'];
            $coin_money=$orderGoods['coin_money'];
            $order_member_id=Db::name("order")->where(['order_id'=>$orderID])->value("member_id");
            $sql="update mm_member_asset set coin=coin+$coin_money,score=score+$score_money where member_id=".$order_member_id;
            $num=Db::query($sql);
            if($num<=0){
                throw new Exception("更新失败");
            }
            Db::commit();
        }catch (Exception $e){
            Db::rollback();
        }
    }

}